<?php
namespace app\index\Controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Order extends Controller
{
    public function _empty(){
      $this->redirect('/');
    }
    public function index(Request $request,$id)
    {
       if((Session::get('login_admin')) ||(Session::get('login_tel'))){
           $input = $request->post();

           //从数据库查询友情链接数据
           $data = Db::table('lt_link')->select();
           //查询导航条数据
           $list = Db::table('lt_index_navigation')->select();
           $house_detail = Db::name('house_details')->where('id',$id)->find();

           if(is_mobile()){
               //显示手机端
               return view('order/Mindex', [
                   'title' => '提交订单页面',
                   'info'=>$list,
                   'data'=>$data,
                   'house_detail'=>$house_detail,
                   'input'=>$input,
               ]);
           }else{
               //显示pc端
               return view('order/index', [
                   'title' => '提交订单页面',
                   'info'=>$list,
                   'data'=>$data,
                   'house_detail'=>$house_detail,
                   'input'=>$input,

               ]);
           }

       }else{
           $this->error('亲,请登录一下','index/login/index');
       }

    }

    public function save(Request $request,$id)
    {
        if(Session::get('login_admin.id')){$user_id=Session::get('login_admin.id');}else{$user_id=Session::get('login_tel.id');}
        $data = $request->post();

        $uuid = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);

        $list = Db::name('house_details')->where('id',$id)->field('houseprice,user_id')->find();
        $total_price = $list['houseprice'] * $data['order_num'];


        //过滤数据f
        $info = [
            "input_date" => $data['input_date'],
            "out_date" => $data['out_date'],
            "order_num" => $data['order_num'],
            "input_person_name" => $data['input_person_name'],
            "total_num" => $data['total_num'],
            "order_email" => $data['order_email'],
            "order_phone" => $data['order_phone'],
            "order_payment" => $data['order_payment'],
            "order_time"=>date('Y-m-d H:i:s',time()),
            "order_no"=>$uuid,
            "total_price"=>$total_price,
            'user_id'=>$user_id,
            'house_id'=>$id,

        ];
        $result = Db::name('order_detials')->insert($info);

        //重定向页面
        if ($result > 0) {
            return $this->success('添加成功', url('index/Ordermanagement/index'));
        } else {
            return $this->error('添加失败');
        }
    }
}